<?php

class FundAction extends PublicAction
{

    public function noticeView()
    {
        /**
         * This function is for viewing the notices in TDX
         */
        $this -> display();
    }

    public function noticeEstablish()
    {
        /**
         * This function is for setting up new notes
         */
    }

    public function ajaxGetValue()
    {
        $fund = D("Fund") -> find();
        echo $fund['total'] / $fund['parts'];
    }

    public function ajaxModify()
    {
        /**
         * Add a record by ajax post method
         * It is for specific usage
         */
        $type = $_POST['type'];
        $amount = $_POST['amount'];
        $str = $_POST['remark'];
        $operID = $_POST['operID'];
        $hash = $_POST['__hash__'];
        $str = D('Fundrecord') -> modify($type, $str, $amount, $operID, $hash);
        $success = false;
        if ($str == "Done!")
            $success = true;
        $this -> ajaxReturn("", ($success) ? "增加记录成功！" : $str, $success);
    }

    public function ajaxGetHistoryValues()
    {
        /**
         * 
         */
    }

    public function ajaxFundStat()
    {
        $user = M('User') -> where('UID = ' . $_SESSION['UID']) -> find();
        echo '真实姓名：' . $user['truename'] . '<br>';
        $fundinfo = M('Fundrecord') -> where('OperID = ' . $_SESSION['UID']) -> select();
        $fundvalue = M('Fund') -> find();
        $sum = 0;
        $sumvalue = 0;
        $CurrentPer = $fundvalue['total'] / $fundvalue['parts'];
        foreach ($fundinfo as $i)
        {
            $sum += $i['parts'];
            if ($i['parts'] <> 0)
                $sumvalue += $i['totalvalue'];
        }
        $sumvalue = sprintf("%.2f", $sumvalue);
        $sum = sprintf("%.2f", $sum);
        $avgcost = sprintf("%.2f", $sumvalue / $sum);
        $Value = sprintf("%.2f", $CurrentPer * $sum);
        $CurrentPer = sprintf("%.2f", $CurrentPer);
        $Winlost = sprintf("%.2f", $CurrentPer - $avgcost);
        echo '持有份额：' . $sum . '<br>';
        echo '市值：' . $Value . '<br>';
        echo '平均成本净值：' . $avgcost . '<br>';
        echo '浮动盈亏：' . $Winlost;
    }

    public function sysRebuildFund()
    {
        $this -> _verifyPrivilege(D('Privilege') -> C('PRIVILEGE_ADMIN'));
        $fundinfo = M('Fundrecord') -> select();
        $sum = 0;
        $sumvalue = 0;
        $sumtotal = 0;
        foreach ($fundinfo as $i)
        {
            $sum += $i['parts'];
            $sumtotal += $i['totalvalue'];
            $sumvalue += $i['value'];
        }
        $fund = M('Fund') -> find();
        $data['parts'] = $sum;
        $data['atotal'] = $sumvalue;
        $data['total'] = $sumtotal;
        M('Fund') -> where("parts = " . $fund['parts']) -> save($data);
    }

    public function jsonGetRecords()
    {
        if ($_POST['history'] == 1)
        {
            //Get Vary History
            $Rec = D("Fundrecord");
            $result = $Rec -> where('rtime >= "' . $_POST['from'] . '" AND rtime <= "'. $_POST['to'].' 23:59:59" AND parts = 0') -> select();
            $this -> ajaxReturn($result);
        }
        else
        {
            
            //Get Buy/Sell History
            $Rec = D("Fundrecord");
            $result = $Rec -> where('rtime >= "' . $_POST['from'] . '"AND parts <> 0 AND rtime <="' . $_POST['to'] . ' 23:59:59" AND OperID = ' . $_SESSION['UID']) -> select();
            $this -> ajaxReturn($result);
        }
    }

    public function jsonGetArticles()
    {
        $page = $_POST['page'];
        $catagory = $_POST['catagory'];
        $level = $_SESSION['PLevel'];
        $perPage = $_POST['per'];
        if($catagory)
            $data = M('Broadcast') -> where('ReqPLevel <= '.$level.' AND catagory = '.$catagory.' AND ForUID = 0') -> limit($per) -> page($page) -> select();
        else
            $data = M('Broadcast') -> where('ReqPLevel <='.$level.' AND ForUID = 0') -> select();
        $User = M('User');
        $Ca = M('Noticetype');
        foreach($data as $i)
        {
            $tmp = $User -> where("UID = ".$i['UID']) -> find();
            $i["UID"] = $tmp['username'];
            $tmp = $Ca -> where('ID = '.$i[catagory]);
            $i['cname'] = $tmp['name'];
        }
        $this -> ajaxReturn($data);
    }
    
    public function jsonGetPersonalArticles()
    {
        $uid = $_SESSION['UID'];
        $data = M('Broadcast') -> where("UID = ".$uid) -> select();
        $this -> ajaxReturn($data);
    }
    
    public function jsonGetArticlesPages()
    {
        $per = $_POST['per'];
        $catagory = $_POST['catagory'];
        if($catagory)$num = M('Broadcast') -> where('catagory = '.$catagory) -> count();
        else $num = M('Broadcast') -> count();
        if($num % $per == 0)
            $this -> ajaxReturn($num / $per);
        else
            $this -> ajaxReturn(floor($num / $per) + 1);
    }
    
    public function download()
    {
        $fileid = $_POST['id'];
        $data = M("Broadcast") -> where("ID = ".$fileid) -> find();
        header("Content-type:application/pdf");
        header("Content-Disposition:attachment;filename='".$data['name'].".pdf'");
        $filename = "./Upload/".$data['filename'];
        if(file_exists($filename))
            readfile($filename);
        else
            $this -> error("您请求的文件不存在，请联系TDX基金经理！");
    }
}

?>